# include <iostream>
# include <cmath>
# include <conio.h>
# include <stdlib.h>
using namespace std;
 int n=7;
 double x[7] = {0.12,0.14,0.16,0.18,0.20,0.22,0.24};
 double y[7] = {6.278,6.404,6.487,6.505,6.436,6.259,5.954};
 double delt(int, int);
 double Stirl(int, double);
int main()
{
  int i,j,d ; double mn,b,q;
    cout<<"\n Enter argyment(x): ";
    cin>>b;
     cout<<"\n***Programma vi4islyet zna4enie v to4ke  x = "<<b<<" *** \n";
     mn=10000;
     for (j=1;j<=n;j++)
     {
        if (fabs(x[j]-b)<mn)
        { mn=fabs(x[j]-b);
          d=j;
        }
     }
     if ((d>(n-4)/2) & (d<=(n+4)/2) & (mn/(x[2]=x[1])<0.25))
        {  q=fabs(b-x[d])/(x[1]-x[0]);
         cout<<"\n Ras4et po formyle stirlinga \n"
             <<"\n rezyltat: "<<Stirl(d,q);
        }
     else  cout<<"\nFormyly stirlinga primenit' nel'zy";
     cout<<" \n";
  system("pause");
}
double delt(int m, int k)
{
 double d;
 if (m==1) { d=y[k+1]-y[k]; }
 else { d=delt(m-1,k+1)-delt(m-1,k); }
 return d;
};
 double Stirl(int d, double q)
{
 double S,Q=1,p1=1, p2=1; int t,i;
 if (d>(n-d)) {t=n-d;} else {t=d;}
 S=y[d];
 for (i=1;i<=t;i++)
 {
  p1=p2*(2*i-1); p2=p1*2*i;
  S=S+(q*Q/p1)*(delt(2*i-1,t-i)+delt(2*i-1,t-i+1))/2;
  S=S+((q*q)*Q/p2)*delt(2*i,t-i);
  Q=Q*(q*q-i*i);
 }
 return S;
}
